package com.csu.tingtian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.csu.tingtian.common.CommonResponse;
import com.csu.tingtian.persistence.FarmRentCommentMapper;
import com.csu.tingtian.service.FarmRentCommentService;
import com.csu.tingtian.vo.FarmRentComment;
import com.csu.tingtian.vo.ProduceComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service("FarmRentCommentService")
public class FarmRentCommentServiceImpl implements FarmRentCommentService {
    @Autowired
    private FarmRentCommentMapper farmRentCommentMapper;
    @Override
    public CommonResponse<FarmRentComment> userSendCommentToProduce(FarmRentComment farmRentComment) {
        int i = farmRentCommentMapper.insert(farmRentComment);
        if (i > -1){
            return CommonResponse.createForSuccess("插入评论成功",farmRentComment);
        }
        else
            return CommonResponse.createForError("插入失败");
    }

    @Override
    public CommonResponse<FarmRentComment> userDeleteComment(String userName, String farmName, String time) {
        FarmRentComment farmRentComment = new FarmRentComment();
        farmRentComment.setUserName(userName);
        farmRentComment.setFarmName(farmName);
        farmRentComment.setTime(time);

        HashMap<String, Object> map = new HashMap<>();
        map.put("userName", userName);
        map.put("farmName",farmName);
        map.put("time",time);
        int result = farmRentCommentMapper.deleteByMap(map);
        if (result > -1){
            return CommonResponse.createForSuccess("删除成功",farmRentComment);
        }
        else
            return CommonResponse.createForError("服务器异常，删除评论失败。");
    }

    @Override
    public CommonResponse<List<FarmRentComment>> userGetCommentByFarmName(String farmName) {
        QueryWrapper<FarmRentComment> farmRentCommentQueryWrapper = new QueryWrapper<>();
        List<FarmRentComment> farmRentCommentList = farmRentCommentMapper.selectList(farmRentCommentQueryWrapper.eq("farmName",farmName));
        if (farmRentCommentList != null){
            return CommonResponse.createForSuccess(farmRentCommentList);
        }
        else
            return CommonResponse.createForError("服务器异常，未查询到相关评论信息");
    }

    @Override
    public CommonResponse<List<String>> returnFarmNameStarIsTwoOrLower() {
        List<String> farmNameList = new ArrayList<>();
        QueryWrapper<FarmRentComment> commentQueryWrapper = new QueryWrapper<>();
        List<FarmRentComment> farmRentCommentList = farmRentCommentMapper.selectList(commentQueryWrapper.le("starLevel",3));
        if (farmRentCommentList != null){
            for (int i = 0; i < farmRentCommentList.size(); i++) {
                String farmName;
                farmName = farmRentCommentList.get(i).getFarmName();
                farmNameList.add(farmName);
            }
            return CommonResponse.createForSuccess(farmNameList);
        }
        else
            return CommonResponse.createForError("未查询到2星及其以下的商家名。");
    }

    @Override
    public int averageStarLevel(String farmName) {
        int star = 0;

        //先根据farmName找出所有的评价
        QueryWrapper<FarmRentComment> queryWrapper = new QueryWrapper<>();
        List<FarmRentComment> list = farmRentCommentMapper.selectList(queryWrapper.eq("farmName",farmName));

        if(list.size() > 0) {
            int num = farmRentCommentMapper.selectCount(queryWrapper.eq("farmName", farmName));

            //每条评论的starlevel加起来
            for (int i = 0; i < list.size(); i++) {
                star = star + list.get(i).getStarLevel();
            }

            //计算平均
            star = star / num;

            return star;
        }
        else
            return 5;
    }
}
